package mis.ds.ps.controller;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import mis.ds.ps.model.dao.impl.DisciplinasDaoImpl;
import mis.ds.ps.model.dao.impl.DocentesDaoImpl;
import mis.ds.ps.model.dao.impl.MapperDaoImpl;
import mis.ds.ps.model.dao.impl.MappercompostoDaoImpl;
import mis.ds.ps.model.dao.impl.ModulosDaoImpl;
import mis.ds.ps.model.dao.impl.VersoesDaoImpl;
import mis.ds.ps.model.iface.Entities;
import mis.ds.ps.model.obj.Disciplinas;
import mis.ds.ps.model.obj.Docentes;
import mis.ds.ps.model.obj.Mapper;
import mis.ds.ps.model.obj.Mappercomposto;
import mis.ds.ps.model.obj.Modulos;
import mis.ds.ps.model.obj.Versoes;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.mysql.jdbc.Connection;

public class DataController {

	private static Logger logger = Logger.getLogger(DataController.class);

	private Connection mysqlConn;
	private DisciplinasDaoImpl disciplinaDao;
	private VersoesDaoImpl versoesDao;
	private MappercompostoDaoImpl mapCompDao;
	private MapperDaoImpl mappersDao;
	private ModulosDaoImpl modulosDao;
	private DocentesDaoImpl docentesDao;

	public DataController(Connection mysqlConn) {

		PropertyConfigurator.configure("log4j.properties");

		// Versoes DAO
		versoesDao = new VersoesDaoImpl();
		versoesDao.setMysqlConn(mysqlConn);

		// MappersComposite DAO
		mapCompDao = new MappercompostoDaoImpl();
		mapCompDao.setMysqlConn(mysqlConn);

		// MappersComposite DAO
		mappersDao = new MapperDaoImpl();
		mappersDao.setMysqlConn(mysqlConn);

		// Disciplinas DAO
		disciplinaDao = new DisciplinasDaoImpl();
		disciplinaDao.setMysqlConn(mysqlConn);

		// Modulos DAO
		modulosDao = new ModulosDaoImpl();
		modulosDao.setMysqlConn(mysqlConn);
		
		// Docentes DAO
		docentesDao = new DocentesDaoImpl();
		docentesDao.setMysqlConn(mysqlConn);

	}

	public List<Versoes> getVersoesOnDB() {
		return versoesDao.getVersoes();
	}

	public List<Mappercomposto> getMapCompOnDB() {
		return mapCompDao.getMappersComp();
	}

	public boolean insertMapperComposto(Mappercomposto mc) {

		if (mapCompDao.insertMapperComposite(mc) > 0) {
			return true;
		}
		return false;
	}

	public int getMapperIdByName(String name) {
		return mapCompDao.selectIDFromMapperComposite(name);
	}

	public List<Mapper> getMappersByCompId(int id) {		
		return mappersDao.getMappersByCompId(id);
	}
	
	public List<Modulos> getModulosOnDB() {
		return modulosDao.getModulos();
	}
	
	public List<Docentes> getDocentesOnDB() {
		return docentesDao.getDocentes();
	}
	

	public boolean commitSimulation(Result result) {

		if (result.getEntity().equalsIgnoreCase(Entities.DISCIPLINAS.getType())) {

			logger.debug("Result as a " + Entities.DISCIPLINAS.getType().toString());

			List<Disciplinas> disciplinaList = new LinkedList<Disciplinas>();

			for (Iterator<String> it = result.getValueMap().keySet().iterator(); it.hasNext();) {
				String attribute = it.next();

				if (attribute.equalsIgnoreCase(Disciplinas.NOME)) {

					for (Object object : result.getValueList(attribute)) {
						Disciplinas disciplina = new Disciplinas();
						if (object instanceof String) {
							disciplina.setNome((String) object);
						}
						logger.debug("Disciplina created " + disciplina);
						disciplinaList.add(disciplina);
					}

				}

				disciplinaDao.insertDisciplinas(disciplinaList);
			}

		}

		return false;
	}

	public void setMysqlConn(Connection mysqlConn) {
		this.mysqlConn = mysqlConn;
	}

	public Connection getMysqlConn() {
		return mysqlConn;
	}

}
